// Copyright 2008 Joshua Suereth
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing
// permissions and limitations under the License. 
  
package scala.tools.eclipse.util

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.core.resources.IProject;
import scala.tools.eclipse.ScalaPlugin;
import org.devzuz.q.maven.embedder.{IMavenProject, MavenCoreActivator}

/** A bunch of objects for matching against IProjects */
object Matching {
  /** Object for extracting Java projects in pattern matching*/
   object JavaProject {
     def unapply(p : IProject) = {
       if (p.hasNature(JavaCore.NATURE_ID)) {
         Some(JavaCore.create(p))
       } else {
         None
       }
     }
   }
   /** Object for extracting Scala Projects in pattern matching */
   object ScalaProject {
     def unapply(p : IProject) : Option[ScalaPlugin#Project] = {
       if(ScalaPlugin.isScalaProject(p)) {
         return ScalaPlugin.plugin.projectSafe(p)
       } else {
         return None;
       }
     }
   }
   
   /** Object for extracting Maven Projects in pattern matching */
   object MavenProject {
     def unapply(p : IProject) : Option[IMavenProject] = {
       try {
         val mp = MavenCoreActivator.getDefault.getMavenProjectManager.getMavenProject(p, false);
         if(mp != null) {
           return Some(mp);
         }
       } catch {
         case t : Throwable => //Do Nothing
       }
       None;
     }
   }
}
